////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.CustomizeDialog;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.more.push.activity.MessageContentActivity;
import cc.telecomdigital.tdstock.activity.groups.more.push.activity.MessageHistoryListActivity;
import cc.telecomdigital.tdstock.activity.groups.more.push.client.Constants;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.Data;
import cc.telecomdigital.tdstock.adapter.MoreItemAdapter;
import cc.telecomdigital.tdstock.adapter.ViewHolderWithIcon;
import cc.telecomdigital.tdstock.trading.Trade_LoginActivity;
import cc.telecomdigital.tdstock.trading.Trade_tradingOptionActivity;
import cc.telecomdigital.tdstock.trading.Services.TradeAppInfo;

/**
 * 
 * @author haibo
 * 
 *  for toolbutton  named  "more"
 *
 */
public class MoreActivity extends BaseActivity
{
	protected static final String	TAG			= MoreActivity.class.getSimpleName();
	private static final long	START_ID		= 90000l;
	
//	public static int _forwardingItemIndex =  -1;

	private Drawable[]			_img;
	private TextView[]			_txt;
	private ListView			_moreListView;
	private static Class<?>[]	ACTIVITY_CLASS	= { null, //index 0
			DXnewsListActivity.class, //index 1 
			FinetListActivity.class, //index 2
			StockADRActivity.class, //index 3 
			MetalActivity.class, // index 4
			OilQuoteActivity.class, //index 5
			AHComparisonActivity.class, //index 6
			InstantMarketActivity.class, //index 7
			null, //index 8   --finish
			ShortSellActivity.class, //index9 
			StockAnalysisActivity.class, //index 10
			FinanceListActivity.class, //index 11
			WarrantsActivity.class, //index 12
			null, //index 13 
//			null, //index 14 horserace
//			null, //index 15 Soccer
//			null, //index 16 六合彩
			TrafficActivity.class, //index 14 車速提示
			CheckUsageActivity.class, //index 15 查閱用量
			StockStarIndexActivity.class, //index 16
			MessageSettingActivity.class,//index 17 警报设定
			MessageHistoryListActivity.class,//index18 推送消息服务（警报记录）
			PriceAlertActivity.class,          //index19 到价提示
			
			ReliefDeclarationActivity.class //index20 免責聲明
										};										// modify by ouyangzhihui 2011-05-24
	private Button				_toolbutton_options;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more);
		
		_toolbutton_options = (Button) findViewById(R.id.toolbtn_more_setting);
		_toolbutton_options.setFocusable(true);
		_toolbutton_options.setClickable(true);
		_toolbutton_options.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(getApplicationContext(), MoreSettingActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(MoreSettingActivity.class, intent);			
			}
		});

		_moreListView = (ListView) findViewById(R.id.toolbutton_more_list);
		_moreListView.setDrawingCacheEnabled(true);
		_moreListView.setScrollbarFadingEnabled(true); // 淡化滾動條
		final String[] txtRes = this.getResources().getStringArray(R.array.toolbuttons_more_list);
		int listSize = txtRes.length;
		_txt = new TextView[listSize];
		_img = new Drawable[listSize];
		for (int index = 0; index < listSize; index++)
		{
			TextView title = new TextView(this);
			title.setText(txtRes[index]);
			_txt[index] = title;
		}
		_img[0] = this.getResources().getDrawable(R.drawable.ic_menu_trade);

		_img[1] = this.getResources().getDrawable(R.drawable.dj);
		_img[2] = this.getResources().getDrawable(R.drawable.finet);
		_img[3] = this.getResources().getDrawable(R.drawable.adr); // modify by ouyangzhihui 2011-07-21
		_img[4] = this.getResources().getDrawable(R.drawable.metal);

		_img[5] = this.getResources().getDrawable(R.drawable.petrol);

		_img[6] = this.getResources().getDrawable(R.drawable.ahcomp);
		_img[7] = this.getResources().getDrawable(R.drawable.market);
		_img[8] = this.getResources().getDrawable(R.drawable.top20);
		_img[9] = this.getResources().getDrawable(R.drawable.shortsell);

		_img[10] = this.getResources().getDrawable(R.drawable.analysis);
		_img[11] = this.getResources().getDrawable(R.drawable.econ);
		_img[12] = this.getResources().getDrawable(R.drawable.warrants);
		_img[13] = this.getResources().getDrawable(R.drawable.ipo);
//		_img[14] = this.getResources().getDrawable(R.drawable.icon_horseback); // by ouyangzhihui  2011-07-04
//		_img[15] = this.getResources().getDrawable(R.drawable.icon_soccer);
//		_img[16] = this.getResources().getDrawable(R.drawable.icon_mk6); // 六合彩
		_img[14] = this.getResources().getDrawable(R.drawable.icon_traffic);
		_img[15] = this.getResources().getDrawable(R.drawable.check_usage); // 查閱用量
		_img[16] = this.getResources().getDrawable(R.drawable.stockstar);
		_img[17]=this.getResources().getDrawable(R.drawable.alertsetting);//警报设定  hunter time 2011-10-31
		_img[18] = this.getResources().getDrawable(R.drawable.alertrecord); // 推送服务    hunter    time 2011-10-12/警报记录  modify hunter 2011-10-31  
		_img[19]=this.getResources().getDrawable(R.drawable.pricealert);  //到价提示

		_img[20] = this.getResources().getDrawable(R.drawable.article32); // 免責聲明
		
		List<ViewHolderWithIcon> list = new ArrayList<ViewHolderWithIcon>();
		for (int index = 0; index < listSize; index++)
		{
			long id = START_ID + index;
			ViewHolderWithIcon viewHolder = new ViewHolderWithIcon();
			viewHolder.id = id;
			viewHolder.text = _txt[index];
			viewHolder.sign = new ImageView(this);
			viewHolder.icon = new ImageView(this);
			viewHolder.icon.setImageDrawable(_img[index]);
			/*if(index == 16)
				viewHolder.icon.setColorFilter(Color.BLACK, Mode.SRC_ATOP);*/
			list.add(viewHolder);
		}

		_messageHandler = null; //this activity no need it.

		_moreListView.setAdapter(new MoreItemAdapter(LayoutInflater.from(this), list));
		_moreListView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				TDStockLog.d(TAG, "position:" + position + "  id:" + id);
				if (position != -1)
				{
					//modified by zhengan
					if (position == 0)
					{						
						if (!TradeAppInfo.TradeApp_Ready)
						{
							new CustomizeDialog( MoreActivity.this.getParent(), 
											getResources().getString( R.string.info),
											"即將推出，敬請關注！").show();						
							return;
						}
					
						boolean isDemoOnly;
						
						//Allow for testing environment
						isDemoOnly = TradeAppInfo.IsAllowDemoOnly(appHost.GetLSClient_ServicePlan());
						if (debugMode)
							TDStockLog.i(TAG, "TradeApp isDemoOnly: " + isDemoOnly);
						
						//If it is a demo account, it must be re-logon
						Class<? extends Activity> primaryAct;
						
						primaryAct = appHost.actListController.GetCurrentActivityClass();
						
						//Flush the keyboard info
						TradeAppInfo.SetStockViewInfoItem(null);
						TradeAppInfo.SetPrimaryEntryClass(primaryAct);
						TradeAppInfo.SetAsBMPMode(appHost.IsBMPMode());

/** Andy	**/
//						//Enfroce for BMP Mode
//						if (appHost.IsBMPMode())
//						{
//							TradeAppInfo.SetAsBMPMode(false);
//							isDemoOnly = true;
//						}
						
						//If it is a demo only flag, user need to logon to replay all the procedure
						if (isDemoOnly)
						{
							TradeAppInfo.SetAsDemoAccount(true);
							TradeAppInfo.SetTradingRegistration(false);
							SwitchActivityFromHost(Trade_LoginActivity.class);
							return;
						}
						
						//Provide a method for use to escape from Demo Mode
						if (TradeAppInfo.IsDemoAccount())
						{
							TradeAppInfo.SetAsDemoAccount(false);
							TradeAppInfo.SetTradingRegistration(false);
							SwitchActivityFromHost(Trade_LoginActivity.class);
							return;
						}							
						
						if (TradeAppInfo.IsTradingRegistered())
						{
							SwitchActivityFromHost(Trade_tradingOptionActivity.class);	
							return;
						}
						
						//A Brand new logon
						TradeAppInfo.SetAsDemoAccount(false);
						SwitchActivityFromHost(Trade_LoginActivity.class);	
						return;
					}

					if (position == 13)
					{
						CommonContentActivity._title = txtRes[position];
						CommonContentActivity._cls = MoreActivity.class;
						
						Intent intent = new Intent(getApplicationContext(), CommonContentActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
						SwitchForwardActivity(CommonContentActivity.class, intent);
						
						return;
					}
					if (position == 8)
					{ 
						// modify by ouyangzhihui 2011-07-21
						((ITDLApplication) getApplicationContext()).putString(CommonDefn.ACTIVITY_CAME_FROM_KEY, TAG);
						
						Intent intent = new Intent(getApplicationContext(), BMPTop20Activity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
						SwitchForwardActivity(BMPTop20Activity.class, intent);
						return;
					}

					//StockADRActivity
//					if (position == 3)
//					{ 
//						// modify by ouyangzhihui 2011-05-24
////						((ITDLApplication) getApplicationContext()).putString(CommonDefn.DATASOURCE_URL, String.format(STOCK_ADR_URL, getWebServer()));
//
//						Intent intent = new Intent(getApplicationContext(), StockADRActivity.class);
//						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
//						
//						SwitchForwardActivity(StockADRActivity.class, intent);
//						
//						return;
//					}

					// modify by ouyangzhihui 2011-07-04
					//by zhengan,足球
//					if(position == 15)
//					{
//						forwardToSoccerActivity();
//						return;
//					}
//					
//					//by zhengan,赛马
//					if(position == 14)
//					{
//						forwardToHorseraceActivity();
//						return;
//					}
//
//					if (position == 16)
//					{
//						forwardToLotteryActivity();
//						return;
//					}
//					
//					//by zhengan,交通
//					if (position == 14)
//					{
//						forwardToTraifficActivity();
//						return;
//					}
					
					if (position == 16)
					{
						StockStarIndexActivity.iFromClass = MoreActivity.class;
					}
					
					//go to push service setting hunter 2011-10-31
                  				 if(position==17)
					{
                	   				forwardPushSettingActivity();
						return;	
					}
					//go to push service   hunter  2011-10-10
					if (position == 18)
					{
						forwardHistoryListActivity();
						return;
					}
					
					if (position ==19)
					{
						forwardPriceAlertActivity();
						return;
					}

					int index = (int) (id - START_ID);
					if (index >= 0 && index < ACTIVITY_CLASS.length)
					{
						if (ACTIVITY_CLASS[index] != null)
						{
							Intent intent = new Intent(getApplicationContext(), ACTIVITY_CLASS[index]);
							intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
							Class<? extends Activity> iActivityClass = (Class<? extends Activity>) ACTIVITY_CLASS[index];
							
							SwitchForwardActivity(iActivityClass, intent);
						}
					}
				}
			}
		});
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		_moreListView.invalidateViews();
		
		if(_messageHandler==null){
			_messageHandler=new Handler();
		}
		_messageHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(Constants._isGo_MessageHistoryListActivity_Immediately) {
					TDStockLog.d(TAG, "_isGo_MessageHistoryListActivity_Immediately:" + Constants._isGo_MessageHistoryListActivity_Immediately);
					Constants._isGo_MessageHistoryListActivity_Immediately=false;
					forwardHistoryListActivity();
					return;
				}
				if(Constants._isGO_MessageContentActivity_Immediately){
					TDStockLog.d(TAG, "_isGO_MessageContentActivity_Immediately:" + Constants._isGO_MessageContentActivity_Immediately);
					Constants._isGO_MessageContentActivity_Immediately=false;
					forwardContentActivity();
				}
			}
		}, 0);
		
		
		
	}
//	
//	protected void  forwardToItem(int  itemIndex) {
//		if(itemIndex  < 0  || itemIndex + 1  > _moreListView.getCount()) 
//		{
//			return;
//		}
//		
//		_moreListView.getChildAt(itemIndex).performClick();
//	}
	
	//by zhengan
	/*
	 * 跳到交通界面
	 */
//	private void forwardToTraifficActivity()
//	{
//		TDStockLog.d(TAG, "forwardToTraifficActivity");
//
//		Intent intent = new Intent(getApplicationContext(), TrafficActivity.class);
//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
//		SwitchForwardActivity(TrafficActivity.class, intent);
//	}
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	/*
	 * by ouyangzhihui 2011-09-30
	 * 六合彩
	 */
//	private void forwardToLotteryActivity()
//	{
//		TDStockLog.d(TAG, "forwardToLotteryActivity");
//		Intent it = new Intent(this, cc.telecomdigital.tdstock.marksix.Activity.TestTabActivity.class);
//		it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//		SwitchActivityFromHost(cc.telecomdigital.tdstock.marksix.Activity.TestTabActivity.class, it);
////		new CustomizeDialog(this.getParent(), "提示", "即將推出，敬請關注!", "確定").show();
//	}



	/**
	 * go to push setting
	 */
	private void forwardPushSettingActivity()
	{
		TDStockLog.d(TAG, "forwardPushActivity");
		Intent intent = new Intent(getApplicationContext(),MessageSettingActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		SwitchForwardActivity(MessageSettingActivity.class, intent);
		
		
	}

           /*enter 2011-10-10
	 * 进入警报历史记录界面
	 */
	private void forwardHistoryListActivity()
	{
		TDStockLog.d(TAG, "forwardHistoryListActivity");
		Intent intent = new Intent(getApplicationContext(),
				MessageHistoryListActivity.class);
		intent.putExtra(Constants.INTENT_KEY_COME_FROM, "");
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		SwitchForwardActivity(MessageHistoryListActivity.class, intent);
	}
	
	
	/**
	 * enter the message content page
	 */
	
	public void forwardContentActivity() 
	{
			TDStockLog.d(TAG, "forwardMessageContentActivity");
   			Intent intent = new Intent(getApplicationContext(),
   			MessageContentActivity.class);
   			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
   			intent.putExtra(Constants.INTENT_KEY_DETAIL_MESSAGE_ID, Data.id);
   			MessageContentActivity.type=1;
   			SwitchForwardActivity(MessageContentActivity.class, intent);
		
	}
	
	
	//到价提示
	private void forwardPriceAlertActivity()
	{
		TDStockLog.d(TAG, "forwardPushActivity");
		Intent intent = new Intent(getApplicationContext(),PriceAlertActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		SwitchForwardActivity(PriceAlertActivity.class, intent);

	}

	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		TDStockLog.d(TAG, "ready to update the intent!");
		getIntent().putExtras(intent);
	}
}
